package com.cssiot.weixin.basic.https;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

public class testHttpClient1 {
	/**
		 * 使用httpPost 参数为JSON格式时(无http摘要认证)
		 */
		public static void testHttpClient1(){
			
			HttpClient httpclient = new DefaultHttpClient();
	
			String url ="http://192.168.10.215:8080/tnserver/property_manager/login";
	        try {
	
	        	HttpPost httppost = new HttpPost(url);
	        	httppost.addHeader("Content-type", "application/x-www-form-urlencoded"); 
	        	//此处略有不同的是，它是直接将JSON格式数据写如到httppost.setEntity实体中
	        	//并采用了StringEntity工具类，来进行转换。。。。
	        	JSONObject json = new JSONObject();
	        	json.put("username", "admin@qq.com");
	        	json.put("password", "123456");
	        	
	        	StringEntity entity = new StringEntity(json.toString(),"UTF-8");
	        	
	        	httppost.setEntity(entity);
	        	
	        	HttpResponse post= httpclient.execute(httppost);
	        
	            if (post.getStatusLine().getStatusCode() == 200) {  
	            	
	            	String conResult =  EntityUtils.toString(post.getEntity());
	            	
	            	JSONObject sobj = new JSONObject();  
	                sobj = JSONObject.fromObject(conResult);
	                System.out.println(sobj);
	            } else {  
	               System.out.println("失败....");
	            }
	        }catch(Exception e){
	        	e.printStackTrace();
	        }finally{
	        	
	        }
		}
}

